﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI;

namespace Wedn.TemplateEngine.UC
{
    public class TemplateHelper// : IDisposable
    {
        ///// <summary>
        ///// 用户控件变量
        ///// </summary>
        //private UserControl userControl;

        /// <summary>
        /// 模版变量
        /// </summary>
        private Template template;

        private Page page;
        /// <summary>
        /// 构造函数
        /// </summary>
        public TemplateHelper()//Page page)
        {
            this.page = (Page)HttpContext.Current.Handler;
            //userControl = new UserControl();
        }
        /// <summary>
        /// 加载一个模板
        /// </summary>
        /// <param name="path">模版文件虚拟路径</param>
        public void Load(string virtualPath)
        {
            //Control c = userControl.ParseControl("<asp:Button ID=\"Button1\" runat=\"server\" Text=\"Button\" />");
            template = page.LoadControl(virtualPath) as Template;
            //template.Response.Write("");
            if (template == null)
            {
                throw (new ArgumentException(virtualPath));
            }
        }
        /// <summary>
        /// 显示模版, 直接返回模版执行结束生成的HTML
        /// </summary>
        /// <returns>返回生成的HTML</returns>
        public string ToHTML()
        {
            TextWriter tw = new StringWriter();
            Render(tw);
            return tw.ToString();
        }

        /// <summary>
        /// 渲染模版到指定流
        /// </summary>
        /// <param name="writer">TextWriter对象,可以传Response.Output</param>
        public void Render(TextWriter writer)
        {
            if (writer == null)
            {
                writer = page.Response.Output;
            }
            HtmlTextWriter htw = new HtmlTextWriter(writer);
            template.RenderControl(htw);
        }
        /// <summary>
        /// 增加一个标签数据
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="obj">值</param>
        public void Put(string key, object obj)
        {
            template.SetDict(new KeyValuePair<string, object>(key, obj));
        }
        /// <summary>
        /// 批量加载标签数据
        /// </summary>
        /// <param name="dict">批量数据字典</param>
        public void Set(Dictionary<string, object> dict)
        {
            template.SetDict(dict);
        }
        ///// <summary>
        ///// 释放资源
        ///// </summary>
        //public void dispose()
        //{
        //    dispose(true);
        //    GC.SuppressFinalize(this);
        //}
        //protected void Dispose(bool disposing)
        //{
        //    if (disposing)
        //    {
        //        userControl.Dispose();
        //    }
        //}
        ///// <summary>
        ///// 析构函数
        ///// </summary>
        //~TemplateHelper()
        //{
        //    Dispose(false);
        //}
    }
}
